﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2012, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Simple Redirect, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports WillStrohl.Modules.SimpleRedirect.VisitorInfoMembers

Namespace WillStrohl.Modules.SimpleRedirect

    <Serializable()> _
    Public NotInheritable Class VisitorInfo
        Implements IVisitorInfo, IHydratable

#Region " Private Members "

        Private p_RedirectId As Integer = Null.NullInteger
        Private p_VisitorId As Integer = Null.NullInteger
        Private p_ReferringUrl As String = Null.NullString
        Private p_ReferringDomain As String = Null.NullString
        Private p_VisitDate As DateTime = Null.NullDate

#End Region

#Region " Public Properties "

        Public Property RedirectId() As Integer Implements IVisitorInfo.RedirectId
            Get
                Return p_RedirectId
            End Get
            Set(ByVal value As Integer)
                p_RedirectId = value
            End Set
        End Property

        Public Property VisitorId() As Integer Implements IVisitorInfo.VisitorId
            Get
                Return p_VisitorId
            End Get
            Set(ByVal value As Integer)
                p_VisitorId = value
            End Set
        End Property

        Public Property ReferringUrl As String Implements IVisitorInfo.ReferringUrl
            Get
                Return p_ReferringUrl
            End Get
            Set(ByVal value As String)
                p_ReferringUrl = value
            End Set
        End Property

        Public Property ReferringDomain As String Implements IVisitorInfo.ReferringDomain
            Get
                Return p_ReferringDomain
            End Get
            Set(ByVal value As String)
                p_ReferringDomain = value
            End Set
        End Property

        Public Property VisitDate() As DateTime Implements IVisitorInfo.VisitDate
            Get
                Return p_VisitDate
            End Get
            Set(value As DateTime)
                p_VisitDate = value
            End Set
        End Property

#End Region

#Region " Constructors "

        Public Sub New()
            ' do nothing
        End Sub

        Public Sub New(ByVal RedirectId As Integer, ByVal ReferringUrl As String, ByVal ReferringDomain As String)
            p_RedirectId = RedirectId
            p_ReferringUrl = ReferringUrl
            p_ReferringDomain = ReferringDomain
        End Sub

        Public Sub New(ByVal VisitorId As Integer, ByVal RedirectId As Integer, ByVal RedirectFrom As String, ByVal RedirectTo As String)
            p_VisitorId = VisitorId
            p_RedirectId = RedirectId
            p_ReferringUrl = ReferringUrl
            p_ReferringDomain = ReferringDomain
        End Sub

#End Region

#Region " IHydratable Implementation "

        Public Property KeyID() As Integer Implements IHydratable.KeyID
            Get
                Return p_VisitorId
            End Get
            Set(ByVal value As Integer)
                p_VisitorId = value
            End Set
        End Property

        Public Sub Fill(ByVal dr As System.Data.IDataReader) Implements IHydratable.Fill
            Try
                While dr.Read
                    If Not dr.Item(RedirectIdField) Is Nothing Then
                        p_RedirectId = Integer.Parse(dr.Item(RedirectIdField).ToString, Globalization.NumberStyles.Integer)
                    End If
                    If Not dr.Item(VisitorIdField) Is Nothing Then
                        p_VisitorId = Integer.Parse(dr.Item(VisitorIdField).ToString, Globalization.NumberStyles.Integer)
                    End If
                    If Not dr.Item(ReferringUrlField) Is Nothing Then
                        p_ReferringUrl = dr.Item(ReferringUrlField).ToString
                    End If
                    If Not dr.Item(ReferringDomainField) Is Nothing Then
                        p_ReferringDomain = dr.Item(ReferringDomainField).ToString
                    End If
                    If Not dr.Item(VisitDateField) Is Nothing Then
                        If IsDate(dr.Item(VisitDateField)) Then
                            p_VisitDate = DateTime.Parse(dr.Item(VisitDateField).ToString())
                        End If
                    End If
                End While
            Catch ex As Exception
                LogException(ex)
            Finally
                If Not dr.IsClosed Then
                    dr.Close()
                End If
            End Try
        End Sub

#End Region

    End Class

End Namespace